#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ifstream;
using std::string;

//一个一个单词读取
void test1()
{
    ifstream ifs("1.test.cc");

    /* cout << ifs.fail() << endl; */
    if(!ifs){
        cerr << "ifstream open file failed" << endl;
        return;
    }

    string word;
    //>>运算符默认以空格符、换行符、
    //制表符为分隔符
    while(ifs >> word)
    {
        cout << word << endl;
    }

    //关闭文件流
    ifs.close();
}

//按行读取
void test2()
{
    string filename("1.test.cc");
    ifstream ifs(filename);

    if(!ifs){
        cerr << "ifstream open file failed" << endl;
        return;
    }
#if 0
    //兼容C语言的写法
    char arr[100] = {0};
    while(ifs.getline(arr,sizeof(arr)))
    {
        cout << arr << endl;
        memset(arr,0,sizeof(arr));
    }
#endif

    //更常用的写法
    string line;
    while(std::getline(ifs,line))
    {
        cout << line << endl;
    }
    
    ifs.close();
}

int main(void){
    test2();
    return 0;
}
